package org.cytoscape.cytargetlinker.internal;

import java.util.Properties;

import org.cytoscape.application.CyApplicationManager;
import org.cytoscape.application.swing.CyAction;
import org.cytoscape.application.swing.CySwingApplication;
import org.cytoscape.application.swing.CytoPanelComponent;
import org.cytoscape.cytargetlinker.internal.dialog.ResultTab;
import org.cytoscape.event.CyEventHelper;
import org.cytoscape.model.CyNetworkFactory;
import org.cytoscape.model.CyNetworkManager;
import org.cytoscape.model.subnetwork.CyRootNetworkManager;
import org.cytoscape.service.util.AbstractCyActivator;
import org.cytoscape.service.util.CyServiceRegistrar;
import org.cytoscape.util.swing.FileUtil;
import org.cytoscape.util.swing.OpenBrowser;
import org.cytoscape.view.layout.CyLayoutAlgorithmManager;
import org.cytoscape.view.model.CyNetworkViewFactory;
import org.cytoscape.view.model.CyNetworkViewManager;
import org.cytoscape.view.vizmap.VisualMappingFunctionFactory;
import org.cytoscape.view.vizmap.VisualMappingManager;
import org.cytoscape.view.vizmap.VisualStyleFactory;
import org.cytoscape.work.TaskFactory;
import org.cytoscape.work.TaskManager;
import org.cytoscape.work.swing.DialogTaskManager;
import org.osgi.framework.BundleContext;

public class CyActivator extends AbstractCyActivator {

	@Override
	public void start(BundleContext context) throws Exception {
		CyApplicationManager cyApplicationManager = getService(context, CyApplicationManager.class);
		CyNetworkViewManager netViewMgr = getService(context, CyNetworkViewManager.class);
		CyNetworkManager netMgr = getService(context, CyNetworkManager.class);
		CyNetworkFactory netFactory = getService(context,CyNetworkFactory.class);
		TaskManager<?, ?> taskMgr = getService(context, TaskManager.class);
		DialogTaskManager dialogTaskManager = getService(context, DialogTaskManager.class);
		TaskFactory taskFactory = getService(context, TaskFactory.class);
		
		CyNetworkViewFactory netViewFactory = getService(context, CyNetworkViewFactory.class);
		CyRootNetworkManager rootNetworkMgr = getService(context, CyRootNetworkManager.class);
		
		CySwingApplication swingApp = getService(context, CySwingApplication.class);
		CyServiceRegistrar serviceRegistrar = getService(context, CyServiceRegistrar.class);
		
		CyLayoutAlgorithmManager layoutMgr = getService(context, CyLayoutAlgorithmManager.class);
		
		VisualStyleFactory visualStyleFactory = getService(context, VisualStyleFactory.class);
		VisualMappingManager visualMappingMgr = getService(context, VisualMappingManager.class);
		VisualMappingFunctionFactory discreteMappingFactory = getService(context, VisualMappingFunctionFactory.class, "(mapping.type=discrete)");
		VisualMappingFunctionFactory continuousMappingFactory = getService(context, VisualMappingFunctionFactory.class, "(mapping.type=continuous)");
		VisualMappingFunctionFactory passThroughMappingFactory = getService(context,VisualMappingFunctionFactory.class, "(mapping.type=passthrough)");
		
		FileUtil fileUtil = getService(context, FileUtil.class);
		OpenBrowser openBrowser = getService(context, OpenBrowser.class);
		CyEventHelper eventHelper = getService(context, CyEventHelper.class);
		
		ResultTab tab = new ResultTab();
		registerService(context,tab,CytoPanelComponent.class, new Properties());
		
		CTLManager manager = new CTLManager(cyApplicationManager, netViewMgr, netMgr, netFactory, taskMgr, netViewFactory, rootNetworkMgr, swingApp, serviceRegistrar, visualStyleFactory, visualMappingMgr, discreteMappingFactory, continuousMappingFactory, passThroughMappingFactory, fileUtil, eventHelper, layoutMgr, dialogTaskManager, taskFactory, context, tab);

		CTLDialogAction dialogAction = new CTLDialogAction(cyApplicationManager, manager, swingApp);
		CTLHelpAction helpAction = new CTLHelpAction("Help", openBrowser);

		registerService(context, dialogAction, CyAction.class, new Properties());
		registerService(context, helpAction, CyAction.class, new Properties());
	}
}
